<?php

/**
 * Created by Command.
 * User: Vito
 */

declare(strict_types=1);

namespace app\logic;

use app\model\PayRecord;
use app\model\RoomLeaseRecord;
use app\model\Housing;
use app\model\PayRecordCopy;

class PayRecordLogic extends BaseLogic
{

    /**
     * 设置模型
     * @return string
     */
    protected function getModelName(): string
    {
        return PayRecord::class;
    }

    /**
     * @Description: 生成租户待缴费记录
     * @author: xiayu <cb1071403016@126.com>
     * @param [type] $response      房间数据
     * @param [type] $params        要添加月份的参数
     * @param [type] $recordData    最新一条房间记录数据
     * @Date: 2022-10-31 11:29:59
     * @version: 1.0.0
     * @return [type]
     */
    public function addLesseeRecord($response, $params)
    {

        $RoomLeaseRecord = new RoomLeaseRecord;
        // $payRecord = new PayRecord;

        $userWhere['room_id'] = $params['room_id'];
        $userWhere['status']  = 1;
        $user_id = $RoomLeaseRecord->getUserRecord($userWhere);



        foreach ($user_id as $key => $value) {
            // echo $value;
            $data = [
                'pid'               => $response['id'],
                'user_id'           => $value,
                'room_id'           => $response['room_id'],
                'fee'               => $response['fee'] / count($user_id),
                'fee_date'          => $response['fee_date'],
                'status'            => $response['status'],
                'm_water'           => $response['m_water'] / count($user_id),
                'water_price'       => $response['water_price'] / count($user_id),
                'm_electricity'     => $response['m_electricity'] / count($user_id),
                'electricity_price' => $response['electricity_price'] / count($user_id),
                'pid'               => $response['id'],
                'mark'              => $response['mark'],
                'water'             => $response['m_water'] / count($user_id),
                'electricity'       => $response['m_electricity'] / count($user_id),
                'room_price'        => $response['room_price']/ count($user_id),
                'property_price'    => $response['property_price'] / count($user_id),
            ];
            $res = payRecord::addLesseeRecord($data);
        }

        return true;
    }

    /**
     * @Description: 获取全部小区
     * @author: xiayu <cb1071403016@126.com>
     * @Date: 2022-11-01 17:11:17
     * @version: 1.0.0
     * @return [type]
     */
    public function getHousing()
    {

        $res = Housing::getDetail();

        return $res;
    }
    /**
     * @Description: 获取总金额
     * @author: xiayu <cb1071403016@126.com>
     * @param [type] $params        //前端参数
     * @Date: 2022-11-02 09:59:14
     * @version: 1.0.0
     * @return [type]
     */   
    public function getPaidTotal($params){

        // print_r($params);
        // die;
        if(isset($params['room_id'])){
            if($params['room_id'] != 0){
            $unPaidWhere[] = ['Room.housing_id','=',$params['room_id']];
            $paidWhere[]   = ['Room.housing_id','=',$params['room_id']];
            }
        }
        if (isset($params['filter'])) {

            $unPaidWhere[] = ['PR.fee_date','between', $params['filter']['fee_date']];
            $paidWhere[] = ['PR.fee_date', 'between', $params['filter']['fee_date']];
            // $paidWhere['Room.housing_id'] = $params['room_id'];
        }
        //  echo '结束了';
        // die;
        $unPaidWhere[] = ['PR.status','=',0];

        $paidWhere[] = ['PR.status', '=', 1];

        $unPaidTotal = PayRecord::getTotal($unPaidWhere);

        $paid        = PayRecord::getTotal($paidWhere);

        $res =  [
            'unPaidTotal' => $unPaidTotal,
            'paid'        => $paid
        ];
        return $res;
    }

    /**
     * @Description: 获取缴费详情
     * @author: xiayu <cb1071403016@126.com>
     * @Date: 2022-11-10 10:46:56
     * @version: 1.0.0
     * @return [type]
     */    
    public static function getUserRecord($params){
        
        if(isset($params['id'])){
            $where['id'] = $params['id'];
        }else{
            $where['user_id'] = $params['user_id'];
            $where['status']   = $params['status'];
        }
        
        
        $res = PayRecord::getRecordDetail($where);

        if($res){
            $res['fee_date'] = date("Y-m-d", $res['fee_date']);
        }
        

        return $res;
    }

    /**
     * @Description: 获取用户全部缴费记录
     * @author: xiayu <cb1071403016@126.com>
     * @param [type] $params
     * @Date: 2022-11-10 15:50:23
     * @version: 1.0.0
     * @return [type]
     */
    public static function getUserRecordList($params){
        $where['user_id'] = $params['user_id'];

        $res = PayRecord::getRecordList($where);
        
        if($res){
            foreach ($res as $key => $value) {
                // print_r($value['fee']);
                $res[$key]['fee_date'] = date("Y-m-d", $value['fee_date']);
            }
        }

        return $res;
    }

    /**
     * @Description: 获取水电表最新读数
     * @author: xiayu <cb1071403016@126.com>
     * @param [type] $params
     * @Date: 2022-11-25 11:13:01
     * @version: 1.0.0
     * @return [type]
     */    
    public function getLastReading($params){
        $where['room_id'] = $params['room_id'];

        $res = PayRecord::where($where)->order('id','desc')->field('water,electricity')->find();

        return $res;
    }

    /**
     * @Description: 获取房间内全部用户的缴费记录
     * @author: xiayu <cb1071403016@126.com>
     * @Date: 2022-11-25 11:13:51
     * @version: 1.0.0
     * @return [type]
     */    
    public function getPidList($where){

        return PayRecord::withoutGlobalScope(['pid'])->where($where)->select();
    }

    /**
     * @Description: 获取要导出的缴费记录
     * @author: xiayu <cb1071403016@126.com>
     * @Date: 2022-12-03 10:30:36
     * @version: 1.0.0
     * @return [type]
     */    
    public function getExportList($params){


        $where[] = ['pid', '<>', 0];
        if(isset($params['housing_id'])){
            // $res = PayRecordCopy::getExportList($where, $params['housing_id']);
            $where[] = ['Room.housing_id', '=', $params['housing_id']];
        }
        if(isset($params['startTime'])){
            $startTime  = $params['startTime'];
            $lastTime   = $params['lastTime'];
            $where[] = ['fee_date', 'between', "$startTime, $lastTime"];
        }
       
        // halt($where);
        $res = PayRecordCopy::getExportList($where);
        foreach ($res as $key => $value) {
            $res[$key]['room_name'] = '未知';
            $res[$key]['real_name'] = '未知';
            if($value['room']){
                $res[$key]['room_name'] = $value['room']['name'];
                if($value['room']['building']){
                    $res[$key]['room_name'] = $value['room']['building_name']. $value['room']['building_num']. $value['room']['name'];
                }
            }
            if($value['User']){
                $res[$key]['real_name'] = $value['User']['real_name'];
            }
            $res[$key]['fee_date']  = date("Y-m-d",$value['fee_date']);

            if($value['status'] == 0){
                $res[$key]['status'] = '待缴费';
            }else{
                $res[$key]['status'] = '已缴费';
            }

        }

        return $res;
    }

    /**
     * @Description: 修改缴费记录信息
     * @author: xiayu <cb1071403016@126.com>
     * @Date: 2022-12-13 15:15:36
     * @version: 1.0.0
     * @return [type]
     */ 
    public function savePayRecord($where,$data){

        return PayRecordCopy::where($where)->update($data);
    }

    /**
     * @Description: 修改缴费记录信息
     * @author: xiayu <cb1071403016@126.com>
     * @Date: 2022-12-13 15:15:36
     * @version: 1.0.0
     * @return [type]
     */ 
    public function getUnpaidRecord($where){

        return PayRecordCopy::where($where)->find();
    }
}
